home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / StringSelection.java < prev    next >
Text File  |  1998-09-22  |  2KB  |  78 lines

  1. /*
  2.  * @(#)StringSelection.java    1.5 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.datatransfer;
  16.  
  17. import java.io.*;
  18.  
  19. /**
  20.  * A class which implements the capability required to transfer a
  21.  * simple java String in plain text format.
  22.  */
  23. public class StringSelection implements Transferable, ClipboardOwner {
  24.  
  25.     final static int STRING = 0;
  26.     final static int PLAIN_TEXT = 1;
  27.  
  28.     DataFlavor flavors[] = {DataFlavor.stringFlavor, DataFlavor.plainTextFlavor};
  29.  
  30.     private String data;
  31.                            
  32.     /**
  33.      * Creates a transferable object capable of transferring the
  34.      * specified string in plain text format.
  35.      */
  36.     public StringSelection(String data) {
  37.         this.data = data;
  38.     }
  39.  
  40.     /**
  41.      * Returns the array of flavors in which it can provide the data.
  42.      */
  43.     public synchronized DataFlavor[] getTransferDataFlavors() {
  44.     return flavors;
  45.     }
  46.  
  47.     /**
  48.      * Returns whether the requested flavor is supported by this object.
  49.      * @param flavor the requested flavor for the data
  50.      */
  51.     public boolean isDataFlavorSupported(DataFlavor flavor) {
  52.     return (flavor.equals(flavors[STRING]) || flavor.equals(flavors[PLAIN_TEXT]));
  53.     }
  54.  
  55.     /**
  56.      * If the data was requested in the "java.lang.String" flavor, return the
  57.      * String representing the selection.
  58.      *
  59.      * @param flavor the requested flavor for the data
  60.      * @exception UnsupportedFlavorException if the requested data flavor is
  61.      *              not supported in the "<code>java.lang.String</code>" flavor.
  62.      */
  63.     public synchronized Object getTransferData(DataFlavor flavor) 
  64.             throws UnsupportedFlavorException, IOException {
  65.     if (flavor.equals(flavors[STRING])) {
  66.         return (Object)data;
  67.     } else if (flavor.equals(flavors[PLAIN_TEXT])) {
  68.         return new StringReader(data);
  69.     } else {
  70.         throw new UnsupportedFlavorException(flavor);
  71.     }
  72.     }
  73.  
  74.     public void lostOwnership(Clipboard clipboard, Transferable contents) {
  75.     }
  76. }
  77.     
  78.